package org.ysu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ysu.common.JsonResult;
import org.ysu.mapper.PoorMapper;
import org.ysu.mapper.RegionMapper;
import org.ysu.pojo.*;
import org.ysu.service.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Filter;

@RestController
@RequestMapping("/family")
public class FamilyController {

    @Autowired
    private FamilyService familyService;
    @Autowired
    private PoorService poorService;
    @Autowired
    private VCadreService vCadreService;
    @Autowired
    private ClueService clueService;
    @Autowired
    private InterviewService interviewService;
    @Autowired
    private RegionService regionService;

    /***
     * 根据family id查询family成员
     */
    @RequestMapping("/getListById/{id}")
    public JsonResult getFamily(@PathVariable Integer id)
    {
        Family family = familyService.getById(id);
        if(family.getFId()==-1){
            return new JsonResult(0,"fail",null);
        }
        List<Poor> poor =poorService.getListsByFid(id);
        return new JsonResult(1,"success",poor);
    }

    /**
     * 根据地区查户
     * 当province、city、country无值时，查询上一级的全部
     * @return
     */
    @RequestMapping("/getListByRegion")
    public JsonResult getFamilyByRegion(@RequestParam("province")String province, @RequestParam("city")String city, @RequestParam("country")String country){
        List<Family> list = familyService.getListByRegion(province, city, country);
        System.out.println(list);
        if(list!=null && list.size()!=0){
            return new JsonResult(1,"success",list);
        }
        return new JsonResult(0,"fail",null);
    }

    /**
     * 根据主键查询住址等信息
     * 暂时无用
     * @param fid
     * @return
     */
    @GetMapping("/get/fid/{fid}")
    public JsonResult getFamilyByFid(@PathVariable Integer fid){
        Family family = familyService.getFamily(fid);
        if(family.getFId()!=-1){
            return new JsonResult(1,"success",family);
        }
        return new JsonResult(0,"fail",null);
    }


    /**
     * 查询所有户，包括头像姓名等多属性
     * @return
     */
    @RequestMapping ("/getAllFamilys")
    public JsonResult getAllFamilys(){
        //需要返回户主姓名、头像、电话、地址和相关简介
        //需要查询两个表格 poor 和 family
        //查询poor表
        List<FamilyList> lists =new ArrayList<>();
        List<Family> list = familyService.getAllFamilys();
        if (list!=null && list.size()!=0){
            //根据户id查询户主和帮助人
            for (int i = 0; i < list.size(); i++) {
                Family family = list.get(i);
                Integer pId = family.getPId();
                //根据pId查询户主姓名和头像、电话、身份证号
                Poor poor = poorService.getPoor(pId);
                FamilyList familyList = new FamilyList();
                familyList.setName(poor.getName());
                familyList.setPhone(poor.getPhone());
                familyList.setAvator(poor.getAvator());
                familyList.setIntroduce(family.getIntroduce());
                familyList.setCountry(family.getCountry());
                familyList.setProvince(family.getProvince());
                familyList.setCity(family.getCity());
                familyList.setTownship(family.getTownship());
                familyList.setVillage(family.getVillage());
                familyList.setId(family.getPId());
                familyList.setFId(family.getFId());
                familyList.setIdnumber(poor.getIdnumber());
                familyList.setPoverty(family.getPoverty());
                //根据vcId查询帮扶人姓名、电话
                Integer vcId = family.getVcId();
                if (vcId!=null){
                    //查询VCadre
                    VCadre vcadre = vCadreService.getById(vcId);
                    //设置familyList相关属性
                    familyList.setHelpName(vcadre.getName());
                    familyList.setHelpPhone(vcadre.getPhone());
                    familyList.setHelpId(vcId);
                }
                lists.add(familyList);
            }
        }
        if(lists != null && lists.size() != 0){
            return new JsonResult(1,"success",lists);
        }
        return new JsonResult(0,"fail",null);
    }

    @GetMapping("/getListByvcid/{vcid}")
    public List<Family> getFamilysByvcid(@PathVariable Integer vcid) {
        return familyService.getFamilysByvcid(vcid);
    }

    /**
     * 返回折线图数据
     * @param data
     * @return
     */
    @RequestMapping("/getPoorLineData/{id}/{data}")
    public JsonResult getPoorLineData(@PathVariable Integer id,@PathVariable Integer data){
        //返回折线图数据，折线图统计五个风险
        return new JsonResult(1,"success",clueService.getRiskData(id,data));
    }

    /**
     * 获取income表数据
     */
    @RequestMapping("/getIncomeAndLiability/{id}/{data}")
    public JsonResult getIncomeData(@PathVariable Integer id,@PathVariable Integer data){
        return new JsonResult(1,"success",interviewService.getInomeAndLiability(id,data));
    }

    /**
     * 新增贫困户
     */
    @RequestMapping("/addPoorFamily")
    public JsonResult addFamily(@RequestBody PoorFamily poorFamily){
        System.out.println(poorFamily);
        familyService.addFamily(poorFamily);
        return null;
    }

    /**
     * 获取地址
     */
    @RequestMapping("/getAddress/{level}/{place}")
    public JsonResult getAddress(@PathVariable Integer level,@PathVariable String place){
        return new JsonResult(1,"success",regionService.getAddress(level,place));
    }


}
